from pickle import FALSE
from web3 import Web3
from web3.middleware import geth_poa_middleware
from enum import Enum
import pymysql
import os
import logging

# set logging format
logging.basicConfig(
  level=logging.INFO,
  format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

IPC_PATH = '/root/goerli/geth.ipc'
PROVIDER_URL = 'https://goerli.infura.io/v3/8b55e04e99464356a003d3b7032504aa'
# WS_URL = 'wss://goerli.infura.io/ws/v3/8b55e04e99464356a003d3b7032504aa'

WHITE_LIST = ['0x147a99698aCd27fc2Cd41469c6e227938890C7bF', '0x146f0D12b71FE9C9Ad253c16bE42AA61738d159d', '0x152D4f16AaF6bF99A06dE4B69dF48eB980Bb67Fc', '0x14D35f6F7D2Ba7b119beEbE9605b2BEf8c2Fe2a6', '0x14A1804723a4Da270D613E597B0f6C40C77fa320', '0x1558a37B972C4E532386BD574fE4367DD75E4E6C', '0x156D0b10a183564a4f15177C68Fc504619b53FbB', '0x1436666064F946CEbF1fd4E56Db6Ce931D9D3B1a', '0x14bbd320c24326196e7aaa61F06f254Ce04A3e54', '0x144037b3b4185Cb58B619a0b5A6d51092Bd9184b', '0x14cef8E55C10B5c7178969fd41AC06E4314D643a', '0x158804F41B49A265D721ce832E65a614403c46F1', '0x14F507AEF7Cf1A9f9Fa406BDCe36e2b5D32244B2', '0x1549598A1d866aA11565Fcf807702d10c1eE228D', '0x14CeBC2FCb06Cfe825576C3f8850C662e47b2055', '0x151AfF9d86cc526D2845Cd1AaA430b2316803F80', '0x14aA200c9C93f985C8c2841F16a2480838c33b8b', '0x158FaFb6ff0bc7f08f1829f446cde64CCe0BD4FF', '0x14cb16d89b6dcCaE25ce198be61d1597fCa732B9', '0x14644909537ADE59C9Ad9E9350648149432ACAbC', '0x146FED3316dF692393a62a0920F9adE3413FEa23', '0x14C77AF06cDc4DEB31e36F57aea5F27AB7Dd19eC', '0x155740C9e87e610D4e5ea78473d4A207A299FB10', '0x152Dd12EBE16dC33827367Fb757B0063385C2874', '0x15773cd10E9fe3Dd6E637bF6CEA65Bdc5d339c12', '0x14948ACa2d458427859E7E116c2307d74d5b96c0', '0x152261D9C29CC583b426e43EAEF696e3E0D349bc', '0x157845ba2A54Ac4b7f8Ef1a4b98cFB19bc117841', '0x14947895103a5Ca29544f17f1EDFf88Fc55c0a35', '0x1442D45FA9dCF4B17B719A43d8466663eD76b5d9', '0x1445b74CDdC206d8277895542e7DF1a0ffD4Be64', '0x158339020Bf8c8B128BFba5716098256417b8ae7', '0x14F876d3F6D29bF68154FE652fe33d84179F8E2e', '0x14d655DA764ECc427C48Ae7F8F4978F0745d4d2A', '0x154b427a1f401CdbA08539689f555729DaC668e1', '0x14B38321453c4Fe912834f0c2dF5ed6D37006C90', '0x14C41Af4De15bc95cF0C274117dCC38CeFfA6EB5', '0x150a9F808931e2E9472fE4117eBB662956D034A0', '0x143679AbdD0Da9cC100FDbd602f98E17630E73CE', '0x14EeB0746ADd286dE3770d2d37E5804a5d91A598', '0x157306C08024aa0F0feD1bF34A158F41B995011a', '0x1481d38743EE53627AeF512553cf13e58941EA78', '0x1451F1db916A24a3953521b4fC9C1140181C6f6d', '0x14D0416B603E87FAB7e9149A2d5Bf7729eb393a6', '0x147d81efd018881e412cAd4B15209D45984D51C2', '0x155fF127BB794dB3a7371f57B224459C4498AE43', '0x143ceA4938156D2d155034d5Dc86AAA75e2F84F5', '0x15885b33B762c3568CbF1EC3d3F8EEbC410198F9', '0x156d379C939d309e4234159Ef6c5C6dD93C2744E', '0x1466977f788Db1EAAc7bAea9d8a2F3dc38FC85d3', '0x155040E0E1C3aCfF263dEC813cE1fD99c5D74C4B', '0x149952bd83d01494395dFcb05BD9605d51Ec1Ae3', '0x147cf0A367fD68912259622449E897bbf347F1fE', '0x14e4fc21C995a60bad1545d1b16D253b4A46A3D2', '0x148dc6e72a5259dB79459d0F4fEd6FCAB7d270D4', '0x14b3De8874560d0eBc1a03F60526dc03b7aa7bD4', '0x143D1f636C65964725FdA27fF5002Ecb734030f6', '0x1437D22B198206b75D0055CBA89aC5A61002E314', '0x154D366D61FB75E5243AF99b9c1Eb06087E257cD']

ALERT_VALUE_IN_ETH = 1000

USE_DATABASE = True
USE_MULTI_PROCESS = False

class ProviderSource(Enum):
  LOCAL_IPC = 0
  INFURA_RPC = 1

def get_w3(source: ProviderSource):
  if source == ProviderSource.LOCAL_IPC:
    w3 = Web3(Web3.IPCProvider(IPC_PATH))
  else: 
    w3 = Web3(Web3.HTTPProvider(PROVIDER_URL))

  w3.middleware_onion.inject(geth_poa_middleware, layer=0)

  return w3

w3 = get_w3(ProviderSource.LOCAL_IPC)
# w3 = get_w3(ProviderSource.INFURA_RPC)

# CREATE TABLE `big_tx` (
# 	`block_number` BIGINT NOT NULL,
# 	`timestamp` BIGINT NOT NULL,
# 	`from_addr` VARCHAR(256) NOT NULL,
# 	`tx_value` DOUBLE NOT NULL
# );

env_dist = os.environ

password = env_dist.get('password')
print(f'using password: {password}')


class TxAlertData:
  def __init__(self, bn, ts, hash, value, from_addr, to_addr, from_balance, to_balance):
    self.bn = bn
    self.ts = ts
    self.hash = hash
    self.value = value
    self.from_addr = from_addr
    self.to_addr = to_addr
    self.from_balance = from_balance
    self.to_balance = to_balance

def get_db():
  return pymysql.connect(
    host='localhost',
    user='root',
    password=password,
    database='big_tx')

def clear_block_record(bn: int):
  db = get_db()
  cursor = db.cursor()
  sql = f"DELETE FROM big_tx.big_tx WHERE block_number={bn}"
  print(f'exec: {sql}')
  cursor.execute(sql)
  db.commit()
  db.close()

def save_alert_data(alert_data: TxAlertData):
  db = get_db()
  cursor = db.cursor()
  block_number = alert_data.bn
  timestamp = alert_data.ts
  hash = alert_data.hash
  tx_value = alert_data.value
  from_addr = alert_data.from_addr
  to_addr = alert_data.to_addr
  from_balance = alert_data.from_balance
  to_balance = alert_data.to_balance
  sql = f"INSERT INTO big_tx.big_tx (`block_number`, `timestamp`, `hash`, `tx_value`, `from_addr`, `to_addr`, `from_balance`, `to_balance`) VALUES ({block_number}, {timestamp}, \"{hash}\", {tx_value}, \"{from_addr}\", \"{to_addr}\", {from_balance}, {to_balance})"
  print(f'exec: {sql}')
  cursor.execute(sql)
  db.commit()
  db.close()

def get_all_data():
  db = get_db()
  cursor = db.cursor()
  sql = f"select * from big_tx.big_tx"
  cursor.execute(sql)
  res = cursor.fetchall()
  db.close()
  return res
